package cn.bx.bid.ctrl;

import cn.bx.bid.entity.Page;
import cn.bx.bid.entity.User;
import cn.bx.bid.service.UserService;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class UserController {
    private static final Logger Log = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    //管理员登陆
    @RequestMapping(value = "ulogin",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String login(@RequestParam("userName") String u, @RequestParam("pass") String p, HttpSession s) {
        User user = userService.login(u, p);
        if (user != null)
            s.setAttribute("USER_LOGIN", user);
        return "{\"msg\":" + (user != null) + ",\"obj\":" + JSON.toJSONStringWithDateFormat(user, "yyyy-MM-dd") + "}";
    }

    //分页，模糊查询
    @RequestMapping(value="usersearch",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Page<User> paging(@RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int limit, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "startDate", required = false) Date startDate, @RequestParam(value = "endDate", required = false) Date endDate) {
        Page<User> paging = this.userService.paging(page, limit, name, startDate, endDate);
        return paging;
    }

    //新增用户
    @RequestMapping(value = "useradd",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String add(@ModelAttribute("u") User u) {
        int r = this.userService.add(u);
        return "{\"msg\":" + (r > 0) + "}";
    }

    //更新用户
    @RequestMapping(value = "userupdate",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String update(@ModelAttribute("u") User u) {
        int r = this.userService.update(u);
        return "{\"msg\":" + (r > 0) + "}";
    }

    //更新用户获取值
    @RequestMapping(value = "userget-{id}",produces = "application/json;charset=utf-8")
    @ResponseBody
    public User get(@PathVariable("id") long id) {
        User user = this.userService.get(id);
        return user;
    }

    //批量删除及单个删除
    @RequestMapping(value = "userbatchdel",produces = "application/json;charset=utf-8")
    @ResponseBody
    public int batchdel(@RequestParam(value = "ids[]")long[] ids){
        int r = this.userService.batchDel(ids);
        return r;
    }

    //验证用户名是否重复
    @RequestMapping(value = "checkUserNameRepeat",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Object checkUserNameRepeat(@RequestParam(value = "id",required = false)Long id,@RequestParam("userName") String userName){
        boolean repeat = this.userService.checkUserNameRepeat(id, userName);
        return repeat;
    }

    @InitBinder
    public void init(WebDataBinder binder) {
        CustomDateEditor dateEditor = new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true);
        binder.registerCustomEditor(Date.class, dateEditor);
    }
}